package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class Setmealiml implements SetmealService {
    /**
     * 套餐起售、停售
     * @param status
     * @param id
     */
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> result = setmealMapper.getSetmeal(dto);
        return new PageResult(result.getTotal(), result.getResult());
    }

    /**
     * 新增菜品
     *
     * @param dto
     */
    @Transactional
    @Override
    public void addSetmeal(SetmealDTO dto) {
        //copy数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);

        setmealMapper.addSetmeal(setmeal);

        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (setmealDishes == null || setmealDishes.isEmpty()) {
            return;
        }

        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });

        setmealMapper.addSetmealDish(setmealDishes);
    }

    /**
     * 修改菜品
     *
     * @param dto
     */
    @Override
    public void updateSetmeal(SetmealDTO dto) {
        //copy数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);

        setmealMapper.updateSetmeal(setmeal);

        List<SetmealDish> setmealDishes = dto.getSetmealDishes();

        if (setmealDishes == null || setmealDishes.isEmpty()) {
            return;
        }
        Set<Long> list = new HashSet<>();
        for (SetmealDish setmealDish : setmealDishes) {
            Long setmealId = setmealDish.getSetmealId();
            list.add(setmealId);
        }
        setmealMapper.deleteSetmealDish(list);
        setmealMapper.addSetmealDish(setmealDishes);
    }

    /**
     * 回显套餐数据
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealById(Long id) {


        SetmealVO setmealVO = setmealMapper.getSetmealById(id);

        List<SetmealDish> list = setmealMapper.getSetmealDishById(id);

        setmealVO.setSetmealDishes(list);

        return setmealVO;
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Transactional
    @Override
    public void deleteSetmeal(List<Long> ids) {
        for (Long id : ids) {

            SetmealVO setmealById = setmealMapper.getSetmealById(id);

            if(setmealById.getStatus()==1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }

        }

        Set<Long> longs = new HashSet<>(ids);
        setmealMapper.deleteSetmeal(ids);

        setmealMapper.deleteSetmealDish(longs);

    }

    /**
     * 套餐起售、停售【
     * @param status
     * @param id
     */
    @Override
    public void ControlDishStatus(Integer status, Long id) {
        //若套餐下的菜品存在停售，则此套餐不能起售

        //根据id查询到所有的套餐中的菜品
        List<Long> list = setmealMapper.getDishId(id);
        for (Long dishId : list) {

            DishVO dish = dishMapper.getDish(dishId);
            if(dish.getStatus()==0){
                throw new SetmealEnableFailedException("套餐中"+ dish.getName()+"已停售");
            }
        }

        Setmeal setmeal = new Setmeal();
        SetmealVO setmealvo = setmealMapper.getSetmealById(id);
        BeanUtils.copyProperties(setmealvo,setmeal);
        setmeal.setStatus(status);

        setmealMapper.updateSetmeal(setmeal);
    }


    /**
     * 用户端条件查询
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> list(Long categoryId) {

        List<Setmeal> list = setmealMapper.list(categoryId);
        List<Setmeal> newList = list.stream().filter(setmeal -> setmeal.getStatus() == StatusConstant.ENABLE).collect(Collectors.toList());

        return newList;
    }

    /**
     * 用户端根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {

        return setmealMapper.getDishItemBySetmealId(id);
    }


}
